package 博客管理系统;

import java.sql.*;
import java.util.*;
import java.awt .*;
import javax.management.Query;
import javax.swing.JFrame;
import java.io.*;
import java.awt.event .*;
import javax.swing .*;
import javax.swing.table .*;

public class A_access extends JFrame {

    JTextArea jta=new JTextArea(6,40);
    JTextField jtf=new JTextField(10);
    JTextField jtf1=new JTextField(10);
    JTextField jtf2=new JTextField(10);
    public A_access() {
        JPanel p=new JPanel();
        JLabel lb=new JLabel("请输入管理员ID:");
        JTextField jtf=new JTextField(10);
        JLabel lb1=new JLabel("请输入用户ID:");
        JLabel lb2=new JLabel("更新等级为:");
        JButton b1=new JButton("更改等级信息");
        b1.addActionListener(new Abc());
        JButton b2=new JButton("关闭");
        b2.addActionListener(e->System.exit(0));

        p.add(lb);
        p.add(jtf);
        p.add(lb1);
        p.add(jtf1);
        p.add(lb2);
        p.add(jtf2);
        p.add(b1);
        p.add(b2);
        p.add(jta);
        getContentPane().add(p);
        setTitle("管理员权限");
        setSize(800,600);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main (String[] args) {
        new  A_access();
    }
    class Abc implements ActionListener{
        public void actionPerformed(ActionEvent e){
            String xh=jtf.getText();
            String xh1=jtf1.getText();
            String xh2=jtf2.getText();
            Connection con;
            Statement st;
            ResultSet rs;
            String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=BlogSystemLD";
            String userName="sa";
            String userPwd="123";
            String sql="select * from UserInfo";
            try {
                con = DriverManager.getConnection(dbURL, userName, userPwd);
                st = con.createStatement();
                rs = st.executeQuery(sql);
                String adminstrators="王老师";
                if(xh.equals(adminstrators)){
                    while (rs.next()) {
                        String user_id = rs.getString(1);
                        String useraccont = rs.getString(2);
                        String userpossword = rs.getString(3);
                        String user_grade= rs.getString(4);
                        if(xh1.equals(user_id)){
                            jta.append("用户ID"+user_id+"\r\n");
                            jta.append("账号"+useraccont +"\r\n");
                            jta.append("密码"+userpossword+"\r\n");
                            jta.append("用户等级"+xh2+"\r\n");
                        }
                    }
                    rs.close();
                    st.close();
                    con.close();
                }
                else{
                    JOptionPane.showMessageDialog(null,"非管理员禁止访问！！！");
                }
            }catch(SQLException exception){
                exception.printStackTrace();
                System.out.println("数据库连接失败！！！");
            }

        }
    }
}
